package architecture.reports;

import java.text.*;
import java.util.*;

import net.sourceforge.jtds.jdbc.*;

public class XmlReport {

	public static final int pageWidth = 595;
	
	public static int columnCount;
	//public static int totalColumnWidth;
	public static int leftMargin;
	public static int rightMargin;
	public static int internWidth;
	
	public static int REPORT_TYPE = 0;
	
	public static void setType(int _type){
		REPORT_TYPE = _type;
	}
	
	public static void setSizes(int _columnCount, int _totalColumnWidth, int _leftMargin)
	{
		columnCount = _columnCount;
		internWidth = _totalColumnWidth;
		leftMargin = _leftMargin;
		rightMargin = pageWidth - internWidth-leftMargin;		
	}
	
	
	public static final String header = "<?xml version=\"1.0\"?><!DOCTYPE jasperReport PUBLIC \"-//JasperReports//DTD JasperReport //EN\" \"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd\">";
	
	private static String atribute(String name, String value){
		return " " + name + "=\"" + value + "\" ";
	}
	
	private static String atribute(String name, int value)
	{
		return atribute(name, ""+value);
	}
	
	public static String jasperStartTag(String name)
	{
		StringBuilder sb = new StringBuilder();
		sb.append("<jasperReport");
		sb.append(atribute("name", name));
		sb.append(atribute("columnCount", 1));
		sb.append(atribute("pageWidth", pageWidth));
		sb.append(atribute("columnWidth", internWidth));
		sb.append(atribute("columnSpacing", 0));
		sb.append(atribute("leftMargin", leftMargin));
		sb.append(atribute("rightMargin", rightMargin));

		
		sb.append("pageHeight=\"842\" topMargin=\"30\"	bottomMargin=\"30\" language=\"java\" orientation=\"Portrait\" " +
				"whenNoDataType=\"NoPages\" printOrder=\"Vertical\"" +
				" isTitleNewPage=\"false\" isSummaryNewPage=\"false\" isFloatColumnFooter=\"false\" whenResourceMissingType=\"Empty\"" +
				" isIgnorePagination=\"false\">");

		return sb.toString();
	}
	
	public static String field(String name, String klasa) { 
		StringBuilder sb = new StringBuilder();
		sb.append("<field");
		sb.append(atribute("name", name));
		sb.append(atribute("class", klasa));
		sb.append("><fieldDescription><![CDATA["+name+"]]></fieldDescription></field>");
		return sb.toString();
	}
	public static String title(String title) {
		return 	"<title> <band height=\"50\"><line>	<reportElement x=\"0\" y=\"0\" width=\""+internWidth+"\" height=\"1\"/>" +
				"<graphicElement/></line><staticText><reportElement x=\"0\" y=\"10\" width=\""+internWidth+"\" height=\"30\"/>" +
				"<textElement textAlignment=\"Center\">	<font size=\"22\"/>	</textElement><text><![CDATA["+title+"]]></text>" +
				"</staticText></band></title>";	
	}
	
	public static String sort(String name) {
		return "<sortField "+atribute("name", name) + atribute("order", "Ascending")+"/>";
	}
	
	
	private static String staticText(String text, int x, int width)
	{
		return "<staticText><reportElement mode=\"Opaque\" isPrintWhenDetailOverflows=\"false\" " +
			atribute("x",x) + atribute("y", 2) + atribute("width",width) + atribute("height", 15)+
				" forecolor=\"#ffffff\" backcolor=\"#777777\"" +
				"/>" +	
				"<text><![CDATA["+text+"]]></text></staticText>";
	}
	
	
	public static String pageHeader(String[] columns, int[] starts, int[] widths, int scale) {
		StringBuilder sb = new StringBuilder();
		sb.append("<pageHeader><band height=\"21\">");
		sb.append("<rectangle><reportElement x=\"0\" y=\"2\" width=\""+internWidth+"\" height=\"15\" backcolor=\"#333333\" />");
		sb.append("<graphicElement pen=\"None\"/></rectangle>");
		for(int i = 0;i<columns.length;i++)
			sb.append(staticText(columns[i], starts[i]*scale, scale*widths[i]-3));
	
		sb.append("<line><reportElement x=\"0\" y=\"20\" width=\""+internWidth+"\" height=\"1\"/><graphicElement/></line></band></pageHeader>");	
		return sb.toString();
	}
	
	private static String textField(String name, String type, int x, int width) {		
		return "<textField><reportElement "+ 
			((REPORT_TYPE == Reporting.HIERARCHY_REPORT_TYPE)?atribute("isPrintRepeatedValues", "false"):"")
				+atribute("x", x) + atribute("width", width)
				+ " y=\"0\" height=\"15\" isPrintWhenDetailOverflows=\"false\" />" +
				"<textFieldExpression class=\""+type+"\"><![CDATA[$F{"+name+"}]]></textFieldExpression></textField>"; 
	}
	
	public static String detail(String[] names, String[] types, int[] starts, int[] widths, int scale) {
		StringBuilder sb = new StringBuilder();
		sb.append("<detail><band height=\"15\">");
		for(int i = 0;i<names.length;i++)
			sb.append(textField(names[i], types[i], starts[i]*scale, widths[i]*scale-3));
		sb.append("</band></detail>");		
		return sb.toString();
	}
	

	public static final String jasperEndTag = "</jasperReport>";
	
	
	private static String getDateTime() {
	   DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	   Date date = new Date();
	   return dateFormat.format(date);
	}
	
	public static String pageFooter() {
		StringBuilder sb = new StringBuilder();
		sb.append("<pageFooter><band height=\"20\"><line><reportElement x=\"0\" y=\"10\" width=\""+internWidth+"\" height=\"1\"/><graphicElement/></line>");
		sb.append(staticText(getDateTime(), 20, 120));
		sb.append("<textField><reportElement x=\"200\" y=\"0\" width=\"80\" height=\"2\"/>");
		sb.append("<textElement textAlignment=\"Right\"/>");
		sb.append("<textFieldExpression class=\"java.lang.String\"><![CDATA[\"Page \" + String.valueOf($V{PAGE_NUMBER}) + \" of\"]]></textFieldExpression>");
		sb.append("</textField><textField evaluationTime=\"Report\"><reportElement x=\"280\" y=\"0\" width=\"75\" height=\"2\"/>");
		sb.append("<textElement/><textFieldExpression class=\"java.lang.String\"><![CDATA[\" \" + String.valueOf($V{PAGE_NUMBER})]]></textFieldExpression>");
		sb.append("</textField></band></pageFooter>");
		return sb.toString();
	}
	
//		<style name="Arial_Normal" isDefault="true" fontName="Arial" fontSize="12" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
//		<style name="Arial_Bold" isDefault="false" fontName="Arial" fontSize="12" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
//		<style name="Arial_Italic" isDefault="false" fontName="Arial" fontSize="12" isBold="false" isItalic="true" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
		

//

//		<pageFooter>
//			<band height="15">
//				<line>
//					<reportElement x="0" y="10" width="515" height="1"/>
//					<graphicElement/>
//				</line>
//				<textField>
//					<reportElement x="200" y="0" width="80" height="2"/>
//					<textElement textAlignment="Right"/>
//					<textFieldExpression class="java.lang.String"><![CDATA["Page " + String.valueOf($V{PAGE_NUMBER}) + " of"]]></textFieldExpression>
//				</textField>
//				<textField evaluationTime="Report">
//					<reportElement x="280" y="0" width="75" height="2"/>
//					<textElement/>
//					<textFieldExpression class="java.lang.String"><![CDATA[" " + String.valueOf($V{PAGE_NUMBER})]]></textFieldExpression>
//				</textField>
//			</band>
//		</pageFooter>

}
